package org.zjx.action.game.inventor;

import org.zjx.analysis.inventor.ItemDescriptionAnalysis;
import org.zjx.analysis.window.InventorAnalysis;
import org.zjx.action.Action;
import org.zjx.action.basic.ScreenshotAction;
import org.zjx.core.IWindow;
import org.zjx.enums.prop.SlotType;
import org.zjx.model.prop.ItemInstance;
import org.zjx.model.prop.ItemTemplate;
import org.zjx.service.PropService;
import org.zjx.service.impl.PropServiceImpl;
import org.zjx.state.AccountState;
import org.zjx.state.ItemState;
import org.zjx.util.StringMatcherUtil;

import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InventorDescriptionAction extends Action {
    private final PropService propService = new PropServiceImpl();
    private final InventorAnalysis inventorAnalysis;

    public InventorDescriptionAction(IWindow targetWindow, InventorAnalysis inventorAnalysis) {
        super(targetWindow, false);
        this.inventorAnalysis = inventorAnalysis;
    }

    @Override
    protected void doExecute() throws Exception {
        ItemState itemState = stateManager.getState(ItemState.class);
        List<ItemInstance> itemInstances = itemState.getItemsByType(SlotType.INVENTORY);
        for (int i = 0; i < itemInstances.size(); i++) {
            ItemInstance itemInstance = itemInstances.get(i);
            if (itemInstance == null || itemInstance.isGather()) continue;
            Rectangle itemArea = inventorAnalysis.getItemIndexArea(i);
            targetWindow.execute(op -> {
                op.gameMove(itemArea.getCenterX(), itemArea.getCenterY());
                op.delay(500);
                return null;
            });
            new ScreenshotAction(targetWindow).execute();
            ItemDescriptionAnalysis itemDescriptionAnalysis = new ItemDescriptionAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
            String ss = itemDescriptionAnalysis.getDescription();
            matchResult(itemInstance, ss);
        }
        AccountState accountState = stateManager.getState(AccountState.class);
        propService.updateItems(accountState.getPlayerId(), itemInstances);
    }


    public void matchResult(ItemInstance instance, String description) {
        ItemTemplate template = propService.getItemTemplate(instance.getTemplateId());

        StringMatcherUtil.MatchResult result = StringMatcherUtil.matchString(template.getDescription(), description);
        if (!result.isMatched()) return;
        Map<String, Object> map = new HashMap<>();
        for (StringMatcherUtil.Keyword keyword : result.getKeywords()) {
            map.put(keyword.getPlaceholder(), keyword.getKeyword());
        }
        if (template.getName().contains("合成旗")) {
            String cityName = (String) map.get("场景");
            targetWindow.execute(op -> {
                op.click(2);
                op.delay(300);
                return null;
            });
            new ScreenshotAction(targetWindow).execute();
            new GatherFlagAction(targetWindow, cityName, map).execute();
        }
        instance.setAttributes(map);
        instance.setGather(true);
    }
}
